from django.contrib import admin
from django.contrib.auth.admin import UserAdmin as BaseUserAdmin
from django.contrib.auth.models import User
from .models import Department, UserProfile, LeaveType, LeaveApplication, ApprovalRecord, LeaveBalance


class UserProfileInline(admin.StackedInline):
    model = UserProfile
    can_delete = False
    verbose_name_plural = '用户配置'


class UserAdmin(BaseUserAdmin):
    inlines = (UserProfileInline,)
    list_display = ('username', 'email', 'first_name', 'last_name', 'get_employee_id', 'get_department', 'is_staff')
    list_filter = ('is_staff', 'is_superuser', 'is_active', 'userprofile__department', 'userprofile__role')
    
    def get_employee_id(self, obj):
        return obj.userprofile.employee_id if hasattr(obj, 'userprofile') else '-'
    get_employee_id.short_description = '工号'
    
    def get_department(self, obj):
        return obj.userprofile.department if hasattr(obj, 'userprofile') else '-'
    get_department.short_description = '部门'


@admin.register(Department)
class DepartmentAdmin(admin.ModelAdmin):
    list_display = ('name', 'description', 'created_at')
    search_fields = ('name',)


@admin.register(UserProfile)
class UserProfileAdmin(admin.ModelAdmin):
    list_display = ('user', 'employee_id', 'department', 'position', 'role', 'supervisor')
    list_filter = ('department', 'role', 'hire_date')
    search_fields = ('user__username', 'user__first_name', 'user__last_name', 'employee_id')
    raw_id_fields = ('supervisor',)


@admin.register(LeaveType)
class LeaveTypeAdmin(admin.ModelAdmin):
    list_display = ('name', 'max_days', 'requires_attachment', 'is_active', 'created_at')
    list_filter = ('is_active', 'requires_attachment')
    search_fields = ('name',)


class ApprovalRecordInline(admin.TabularInline):
    model = ApprovalRecord
    extra = 0
    readonly_fields = ('created_at',)


@admin.register(LeaveApplication)
class LeaveApplicationAdmin(admin.ModelAdmin):
    list_display = ('applicant', 'leave_type', 'start_date', 'end_date', 'days_requested', 'status', 'created_at')
    list_filter = ('status', 'leave_type', 'start_date', 'created_at')
    search_fields = ('applicant__username', 'applicant__first_name', 'applicant__last_name', 'reason')
    readonly_fields = ('days_requested', 'created_at', 'updated_at', 'submitted_at', 'approved_at')
    inlines = [ApprovalRecordInline]
    
    fieldsets = (
        ('基本信息', {
            'fields': ('applicant', 'leave_type', 'start_date', 'end_date', 'days_requested', 'reason')
        }),
        ('附加信息', {
            'fields': ('attachment', 'emergency_contact', 'emergency_phone', 'work_handover')
        }),
        ('审批信息', {
            'fields': ('status', 'supervisor_approver', 'hr_approver')
        }),
        ('时间信息', {
            'fields': ('created_at', 'updated_at', 'submitted_at', 'approved_at'),
            'classes': ('collapse',)
        }),
    )


@admin.register(ApprovalRecord)
class ApprovalRecordAdmin(admin.ModelAdmin):
    list_display = ('leave_application', 'approver', 'action', 'created_at')
    list_filter = ('action', 'created_at')
    search_fields = ('leave_application__applicant__username', 'approver__username')
    readonly_fields = ('created_at',)


@admin.register(LeaveBalance)
class LeaveBalanceAdmin(admin.ModelAdmin):
    list_display = ('user', 'leave_type', 'year', 'total_days', 'used_days', 'remaining_days')
    list_filter = ('leave_type', 'year')
    search_fields = ('user__username', 'user__first_name', 'user__last_name')


# 重新注册User模型
admin.site.unregister(User)
admin.site.register(User, UserAdmin)
